package com.linwancun.lwcyback.controller;

import com.linwancun.lwcyback.model.*;
import com.linwancun.lwcyback.service.*;
import com.linwancun.lwcyback.util.JsonTransformation;
import com.linwancun.lwcyback.util.Token;
import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jwts;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.xml.bind.DatatypeConverter;
import java.util.Date;
import java.util.List;

@RestController
@EnableAutoConfiguration
/**
 *@类名 BackController
 *@描述：后台控制器
 *@作者：高国彬
 *@日期 2019-07-19 11:54
 *@版本:1.0
 **/

public class BackController {
    @Autowired(required = false)
    private ManagerServiceImp managerService;
    @Autowired(required = false)
    private HuatiServiceImp huatiService;
    @Autowired(required = false)
    private HospitalServiceImpl hospitalService;
    @Autowired(required = false)
    private PatientServiceImp patientService;
    @Autowired(required = false)
    private DoctorServiceImp doctorService;
    @Autowired(required = false)
    private ChatRecordServiceImpl chatRecordService = new ChatRecordServiceImpl();

    @Autowired(required = false)
    private DoctorServiceImp doctorServiceImp = new DoctorServiceImp();


    @Autowired(required = false)
    private HuatiServiceImp huatiServiceImp = new HuatiServiceImp();

    @Autowired(required = false)
    private PatientServiceImp patientServiceImp = new PatientServiceImp();

    @Autowired(required = false)
    private SymptomaticServiceImp symptomaticServiceImp = new SymptomaticServiceImp();

    @Autowired
    private HttpServletRequest request;

    @GetMapping("/back/checktoken")
    /*
     * @作者 高国彬
     * @日期 2019-07-19 11:58
     * @param
     * @return java.lang.String
     * @throws
     * @since
     */

    public String checktoken() {
        try {
            Manager manager = new Manager();
            String jwt = (String) request.getHeader("token");

            Claims claims = Jwts.parser()
                    .setSigningKey(DatatypeConverter.parseBase64Binary("4234dfasdfqdd"))
                    .parseClaimsJws(jwt).getBody();
            manager.setManageraccountnumber(claims.getId());
            System.out.println(claims.getId());


            Manager foundmanager = managerService.find(manager);

            if (foundmanager == null && !claims.getSubject().equals("manager") && claims.getExpiration().before(new Date()))
                return "404";
            return JsonTransformation.objectToJson(foundmanager);
        } catch (Exception e) {
            return "404";
        }

    }

    @PostMapping("/back/login")
    @ResponseBody
    /*
     * @作者 高国彬
     * @日期 2019-07-19 11:58
     * @param str
     * @return java.lang.String
     * @throws
     * @since
     */

    public String backLogin(@RequestBody String str) {
        Manager manager = JsonTransformation.jsonToObject(str, Manager.class);
        Manager foundManager = managerService.find(manager);
        Token t = new Token();

        String jwt = t.createJWT(manager.getManageraccountnumber(), "lwcy", "manager", 60 * 60 * 24 * 3);

        TokenCharactor tokenCharactor = JsonTransformation.jsonToObject(str, TokenCharactor.class);
        tokenCharactor.setManager(foundManager);
        tokenCharactor.setToken(jwt);

        if (foundManager == null)
            return "404";
        return JsonTransformation.objectToJson(tokenCharactor);
    }

    //添加话题
    @PostMapping("/back/inserttopic")
    @ResponseBody
    /*
     * @作者 高国彬
     * @日期 2019-07-19 11:58
     * @param str
     * @return java.lang.String
     * @throws
     * @since
     */

    public String insertTopic(@RequestBody String str) {
        Huati huati = JsonTransformation.jsonToObject(str, Huati.class);
        try {
            huatiService.insert(huati);
        } catch (Exception e) {
            return "404";
        }
        return "200";
    }

    //添加医院
    @PostMapping("/back/inserthospital")
    @ResponseBody
    /*
     * @作者 高国彬
     * @日期 2019-07-19 11:58
     * @param str
     * @return java.lang.String
     * @throws
     * @since
     */

    public String insertHospital(@RequestBody String str) {
        Hospital hospital = JsonTransformation.jsonToObject(str, Hospital.class);
        try {
            hospitalService.insert(hospital);
        } catch (Exception e) {
            return "404";
        }
        return "200";
    }

    //获取患者列表
    @GetMapping("/back/getpatientlist")
    @ResponseBody
    /*
     * @作者 高国彬
     * @日期 2019-07-19 11:58
     * @param
     * @return java.lang.String
     * @throws
     * @since
     */

    public String getPatientList() {
        try {
            List<Patient> list = patientService.getAll();
            String patientList = JsonTransformation.listToJson(list);
            return patientList;
        } catch (Exception e) {
            return "404";
        }
    }

    //获取医生列表
    @GetMapping("/back/getdoctorlist")
    @ResponseBody
    /*
     * @作者 高国彬
     * @日期 2019-07-19 11:58
     * @param
     * @return java.lang.String
     * @throws
     * @since
     */

    public String getDoctorList() {
        try {
            List<Doctor> list = doctorService.getAll();
            String doctorList = JsonTransformation.listToJson(list);
            return doctorList;
        } catch (Exception e) {
            return "404";
        }
    }

    //获取病人详细信息
    @PostMapping("/back/getpatientinfo")
    @ResponseBody
    /*
     * @作者 高国彬
     * @日期 2019-07-19 11:58
     * @param str
     * @return java.lang.String
     * @throws
     * @since
     */

    public String getPatientInfo(@RequestBody String str) {
        Patient patient = JsonTransformation.jsonToObject(str, Patient.class);
        Patient foundPatient = patientService.find(patient);
        if (foundPatient == null)
            return "404";
        String patientInfo = JsonTransformation.objectToJson(foundPatient);
        return patientInfo;
    }

    //获取医生详细信息
    @PostMapping("/back/getdoctorinfo")
    @ResponseBody
    /*
     * @作者 高国彬
     * @日期 2019-07-19 11:58
     * @param str
     * @return java.lang.String
     * @throws
     * @since
     */

    public String getDoctorInfo(@RequestBody String str) {
        Doctor doctor = JsonTransformation.jsonToObject(str, Doctor.class);
        Doctor foundDoctor = doctorService.find(doctor);
        if (foundDoctor == null)
            return "404";
        String doctorInfo = JsonTransformation.objectToJson(foundDoctor);
        return doctorInfo;
    }

    //获取统计数据
    @GetMapping("/back/getstaticinfo")
    @ResponseBody
    /*
     * @作者 高国彬
     * @日期 2019-07-19 11:59
     * @param
     * @return java.lang.String
     * @throws
     * @since
     */

    public String getStaticInfo() {
        return JsonTransformation.objectToJson(getdata());
    }

    /*
     * @作者 高国彬
     * @日期 2019-07-19 11:59
     * @param null
     * @return
     * @throws
     * @since
     */

    public StaticData getdata() {
        Patient malePatient = new Patient();
        malePatient.setPatientsex("男");
        Patient femalePatient = new Patient();
        femalePatient.setPatientsex("女");

        Doctor yishi = new Doctor();
        yishi.setPosition("医师");
        Doctor zhuzhiyishi = new Doctor();
        zhuzhiyishi.setPosition("主治医师");
        Doctor zhurenyishi = new Doctor();
        zhurenyishi.setPosition("主任医师");
        Doctor fuzhurenyishi = new Doctor();
        fuzhurenyishi.setPosition("副主任医师");
        Doctor maleDoctor = new Doctor();
        maleDoctor.setDoctorsex("男");
        Doctor femaleDoctor = new Doctor();
        femaleDoctor.setDoctorsex("女");

        Chatrecord ptodchat = new Chatrecord();
        ptodchat.setIspatienttodoctor(1);
        Chatrecord dtopchat = new Chatrecord();
        dtopchat.setIspatienttodoctor(0);

        List<Patient> patientList = patientServiceImp.getAll();
        List<Patient> malePatientList = patientServiceImp.getList(malePatient);
        List<Patient> femalePatientList = patientServiceImp.getList(femalePatient);

        List<Doctor> doctorList = doctorServiceImp.getAll();
        List<Doctor> maleDoctorList = doctorServiceImp.getList(maleDoctor);
        List<Doctor> femaleDoctorList = doctorServiceImp.getList(femaleDoctor);
        List<Doctor> yishiList = doctorServiceImp.getList(yishi);
        List<Doctor> zhuzhiyishilist = doctorServiceImp.getList(zhuzhiyishi);
        List<Doctor> zhurenyishilist = doctorServiceImp.getList(zhurenyishi);
        List<Doctor> fuzhurenyishilist = doctorServiceImp.getList(fuzhurenyishi);

        List<Hospital> hospitalList = hospitalService.getAll();

        List<Huati> huatiList = huatiServiceImp.getAll();

        List<Symptomatic> symptomaticList = symptomaticServiceImp.getAll();

        List<Chatrecord> chatrecordList = chatRecordService.getAll();
        List<Chatrecord> ptodchatlist = chatRecordService.getList(ptodchat);
        List<Chatrecord> dtopchatlist = chatRecordService.getList(dtopchat);


        StaticData staticData = new StaticData();
        staticData.setPatientNumber(patientList.size());
        staticData.setPatientMaleNumber(malePatientList.size());
        staticData.setPatientFemaleNumber(femalePatientList.size());

        staticData.setDoctorNumber(doctorList.size());
        staticData.setPhysicianNumber(yishiList.size());
        staticData.setAttendingDoctorNumber(zhuzhiyishilist.size());
        staticData.setChiefPhysicianNumber(zhurenyishilist.size());
        staticData.setDeputyChiefPhysicianNumber(fuzhurenyishilist.size());
        staticData.setDoctorMaleNumber(maleDoctorList.size());
        staticData.setDoctorFemaleNumber(femaleDoctorList.size());

        staticData.setSypmtonmaticNumber(symptomaticList.size());

        staticData.setHospitalNumber(hospitalList.size());
        staticData.setTopicNumber(huatiList.size());

        staticData.setChatNumber(chatrecordList.size());
        staticData.setPatientToDoctorChatNumber(ptodchatlist.size());
        staticData.setDoctorToPatientNumber(dtopchatlist.size());
        return staticData;

    }
}
